package com.houweishi.module.receipt.service.impl;

import java.util.List;
import com.houweishi.common.utils.DateUtils;
import com.houweishi.module.outbound.domain.BizOutboundProduct;
import com.houweishi.module.product.domain.BizProduct;
import com.houweishi.module.product.domain.BizProductModel;
import com.houweishi.module.product.mapper.BizProductMapper;
import com.houweishi.module.product.mapper.BizProductModelMapper;
import com.houweishi.module.receipt.domain.BizReceiptProduct;
import com.houweishi.module.receipt.mapper.BizReceiptProductMapper;
import com.houweishi.module.stock.domain.BizStock;
import com.houweishi.module.stock.mapper.BizStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.houweishi.module.receipt.mapper.BizReceiptMapper;
import com.houweishi.module.receipt.domain.BizReceipt;
import com.houweishi.module.receipt.service.IBizReceiptService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 入库记录Service业务层处理
 * 
 * @author houweishi
 * @date 2023-02-25
 */
@Service
public class BizReceiptServiceImpl implements IBizReceiptService 
{
//    @Autowired
    @Resource
    private BizReceiptMapper bizReceiptMapper;

    @Resource
    private BizReceiptProductMapper bizReceiptProductMapper;

    @Resource
    private BizStockMapper bizStockMapper;

    @Resource
    private BizProductModelMapper bizProductModelMapper;

    @Resource
    private BizProductMapper bizProductMapper;

    /**
     * 查询入库记录
     * 
     * @param id 入库记录主键
     * @return 入库记录
     */
    @Override
    public BizReceipt selectBizReceiptById(Long id)
    {
        BizReceipt bizReceipt = bizReceiptMapper.selectBizReceiptById(id);
        BizReceiptProduct bizReceiptProduct = new BizReceiptProduct();
        bizReceiptProduct.setReceiptId(id);
        List<BizReceiptProduct> bizReceiptProductList = bizReceiptProductMapper.selectBizReceiptProductList(bizReceiptProduct);

        bizReceipt.setBizReceiptProductList(bizReceiptProductList);
        return bizReceipt;
    }

    /**
     * 查询入库记录列表
     * 
     * @param bizReceipt 入库记录
     * @return 入库记录
     */
    @Override
    public List<BizReceipt> selectBizReceiptList(BizReceipt bizReceipt)
    {
        List<BizReceipt> list = bizReceiptMapper.selectBizReceiptList(bizReceipt);

        if(!CollectionUtils.isEmpty(list)){
            list.forEach(receipt->{
                BizReceiptProduct bizReceiptProduct = new BizReceiptProduct();
                bizReceiptProduct.setReceiptId(receipt.getId());
                List<BizReceiptProduct> bizReceiptProductList = bizReceiptProductMapper.selectBizReceiptProductList(bizReceiptProduct);
                receipt.setBizReceiptProductList(bizReceiptProductList);
            });
        }
        return list;
    }

    /**
     * 新增入库申请
     * 
     * @param bizReceipt 入库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizReceipt(BizReceipt bizReceipt)
    {
        bizReceipt.setCreateTime(DateUtils.getNowDate());
        bizReceipt.setStatus("1");
        int result = bizReceiptMapper.insertBizReceipt(bizReceipt);

        //插入从表
        if(!CollectionUtils.isEmpty(bizReceipt.getBizReceiptProductList())) {
            bizReceipt.getBizReceiptProductList().forEach(product->{
                product.setReceiptId(product.getId());
                bizReceiptProductMapper.insertBizReceiptProduct(product);
            });
        }

        return result;
    }

    /**
     * 库管直接新增入库
     *
     * @param bizReceipt 入库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizReceipt2(BizReceipt bizReceipt)
    {
        bizReceipt.setCreateTime(DateUtils.getNowDate());
        int result = bizReceiptMapper.insertBizReceipt(bizReceipt);

        //插入从表
        if(!CollectionUtils.isEmpty(bizReceipt.getBizReceiptProductList())) {
            bizReceipt.getBizReceiptProductList().forEach(product->{
                product.setReceiptId(bizReceipt.getId());
                bizReceiptProductMapper.insertBizReceiptProduct(product);

                //修改库存表中的产品数量
                BizStock bizStock = new BizStock();
                bizStock.setWarehouseId(product.getWarehouseId());
                bizStock.setProductId(Long.valueOf(product.getProductId()));
                List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                //如果机器所入的仓库有这个类型的机器，则库存数量+入库数量，如果没有，则创建这一类型，现存数量为入库数量。
                if(!CollectionUtils.isEmpty(bizStockList)){
                    bizStock = bizStockList.get(0);
                    bizStock.setQuantity(bizStock.getQuantity() + product.getNumber());
                    bizStockMapper.updateBizStock(bizStock);
                }else{
                    BizStock stock2 = new BizStock();
                    stock2.setProductName(product.getProductName());
                    stock2.setProductId(Long.valueOf(product.getProductId()));
                    stock2.setQuantity(product.getNumber());
                    stock2.setProductType(product.getProductType());
                    stock2.setWarehouseId(product.getWarehouseId());
                    stock2.setWarehouseName(product.getWarehouseName());
                    bizStockMapper.insertBizStock(stock2);
                }

                //修改产品种类表中的产品数量
                BizProductModel productModel = new BizProductModel();
                productModel.setProductId(product.getProductId());
                //productModel.setModel(product.getModel());
                List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                //如果所入的物品在产品模型中则库存数量+入库数量，如果没有，则创建这一类型，现存数量为入库数量,同时改变产品数量。
                if(!CollectionUtils.isEmpty(productModelList)){
                    BizProductModel productModel1 = productModelList.get(0);
                    productModel1.setQuantity(productModel1.getQuantity() + product.getNumber());
                    bizProductModelMapper.updateBizProductModel(productModel1);

                    BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                    bizProduct1.setQuantity(bizProduct1.getQuantity() + product.getNumber() );
                    bizProductMapper.updateBizProduct(bizProduct1);
                } else{
                    BizProductModel productModel2 = new BizProductModel();
                    productModel2.setProductName(product.getProductName());
                    productModel2.setProductId(product.getProductId());
                    productModel2.setQuantity(product.getNumber());
                    productModel2.setProductType(product.getProductType());
//                    productModel2.setWarehouseId(product.getWarehouseId());
//                    productModel2.setWarehouseName(product.getWarehouseName());
                    bizProductModelMapper.insertBizProductModel(productModel2);

                    BizProduct bizProduct2 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                    bizProduct2.setQuantity(bizProduct2.getQuantity() + product.getNumber() );
                    bizProductMapper.updateBizProduct(bizProduct2);
                }

            });
        }

        return result;
    }


    /**
     * 修改入库申请
     *
     * @param bizReceipt 入库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizReceipt(BizReceipt bizReceipt)
    {
        bizReceipt.setUpdateTime(DateUtils.getNowDate());

        //先删后增
        bizReceiptProductMapper.deleteBizReceiptProductByReceiptId(bizReceipt.getId());
        //插入从表
        if(!CollectionUtils.isEmpty(bizReceipt.getBizReceiptProductList())) {
            bizReceipt.getBizReceiptProductList().forEach(product->{
                product.setReceiptId(product.getId());
                bizReceiptProductMapper.insertBizReceiptProduct(product);
            });
        }
        return bizReceiptMapper.updateBizReceipt(bizReceipt);
    }

    /**
     * 同意通过入库申请后，修改状态和库存
     *
     * @param bizReceipt 入库记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBizReceipt2(BizReceipt bizReceipt)//只需要传id和状态
    {
        bizReceipt.setUpdateTime(DateUtils.getNowDate());
        bizReceipt.setReturnTime(DateUtils.getNowDate());
        bizReceipt.setStatus("2");
        int result = bizReceiptMapper.updateBizReceipt(bizReceipt);

        //查询从表记录
        BizReceiptProduct bizReceiptProduct = new BizReceiptProduct();
        bizReceiptProduct.setReceiptId(bizReceipt.getId());
        List<BizReceiptProduct> receiptProductList = bizReceiptProductMapper.selectBizReceiptProductList(bizReceiptProduct);


        //插入从表
        if(!CollectionUtils.isEmpty(receiptProductList)) {
            receiptProductList.forEach(product->{
                //如果此时状态值为2
                if(bizReceipt.getStatus().equals("2")) {
                    //修改库存表中的产品数量
                    BizStock bizStock = new BizStock();
                    bizStock.setWarehouseId(bizReceipt.getWarehouseId());
                    bizStock.setProductId(Long.valueOf(product.getProductId()));
                    List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                    if (!CollectionUtils.isEmpty(bizStockList)) {
                        bizStock = bizStockList.get(0);
                        bizStock.setQuantity(bizStock.getQuantity() + product.getNumber());
                        bizStockMapper.updateBizStock(bizStock);
                    }

                    //修改产品种类表中的产品数量
                    BizProductModel productModel = new BizProductModel();
                    productModel.setProductId(product.getProductId());
                    List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                    BizProductModel productModel1 = productModelList.get(0);
                    productModel.setQuantity(productModel1.getQuantity() + product.getNumber());
                    bizProductModelMapper.updateBizProductModel(productModel1);

                    //修改产品表数量
                    BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                    bizProduct1.setQuantity(bizProduct1.getQuantity() + product.getNumber() );
                    bizProductMapper.updateBizProduct(bizProduct1);
                }
            });
        }
        return result;
    }

    /**
     * 批量删除入库记录
     * 
     * @param ids 需要删除的入库记录主键
     * @return 结果
     */
    @Override
    public int deleteBizReceiptByIds(Long[] ids)
    {
        return bizReceiptMapper.deleteBizReceiptByIds(ids);
    }

    /**
     * 删除入库记录信息
     * 
     * @param id 入库记录主键
     * @return 结果
     */
    @Override
    public int deleteBizReceiptById(Long id)
    {
        return bizReceiptMapper.deleteBizReceiptById(id);
    }

    /**
     * 修改入库申请
     *
     * @param bizReceipt 入库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizReceipt3(BizReceipt bizReceipt){
        bizReceipt.setUpdateTime(DateUtils.getNowDate());

        //先查出之前的记录把库存还原
        BizReceiptProduct bizReceiptProduct = new BizReceiptProduct();
        bizReceiptProduct.setReceiptId(bizReceipt.getId());
        List<BizReceiptProduct> bizReceiptProductList = bizReceiptProductMapper.selectBizReceiptProductList(bizReceiptProduct);
        if(!CollectionUtils.isEmpty(bizReceiptProductList)) {
            bizReceiptProductList.forEach(product->{

                //还原之前对库存表中物品数量的修改
                BizStock bizStock = new BizStock();
                bizStock.setWarehouseId(product.getWarehouseId());
                bizStock.setProductId(Long.valueOf(product.getProductId()));
                List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                if(!CollectionUtils.isEmpty(bizStockList)){
                    bizStock = bizStockList.get(0);
                    bizStock.setQuantity(bizStock.getQuantity() - product.getNumber());
                    bizStockMapper.updateBizStock(bizStock);
                }

                //还原之前对产品种类表中数量的修改
                BizProductModel productModel = new BizProductModel();
                productModel.setProductId(product.getProductId());
                List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                BizProductModel productModel1 = productModelList.get(0);
                productModel1.setQuantity(productModel1.getQuantity() - product.getNumber());
                bizProductModelMapper.updateBizProductModel(productModel1);

                //修改产品表数量
                BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                bizProduct1.setQuantity(bizProduct1.getQuantity() - product.getNumber() );
                bizProductMapper.updateBizProduct(bizProduct1);
            });
        }

        //先删之前的入库产品记录然后再新增修改后的
        bizReceiptProductMapper.deleteBizReceiptProductByReceiptId(bizReceipt.getId());

        if(!CollectionUtils.isEmpty(bizReceipt.getBizReceiptProductList())) {
            bizReceipt.getBizReceiptProductList().forEach(product->{
                product.setReceiptId(bizReceipt.getId());
                bizReceiptProductMapper.insertBizReceiptProduct(product);

                //再次修改库存表中的产品数量
                BizStock bizStock = new BizStock();
                bizStock.setWarehouseId(product.getWarehouseId());
                bizStock.setProductId(Long.valueOf(product.getProductId()));
                List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                if(!CollectionUtils.isEmpty(bizStockList)){
                    bizStock = bizStockList.get(0);
                    bizStock.setQuantity(bizStock.getQuantity() + product.getNumber());
                    bizStockMapper.updateBizStock(bizStock);
                }

                //再次修改产品种类表中的产品数量
                BizProductModel productModel = new BizProductModel();
                productModel.setProductId(product.getProductId());
                List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                BizProductModel productModel1 = productModelList.get(0);
                productModel1.setQuantity(productModel1.getQuantity() + product.getNumber());
                bizProductModelMapper.updateBizProductModel(productModel1);

                //修改产品表数量
                BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                bizProduct1.setQuantity(bizProduct1.getQuantity() + product.getNumber() );
                bizProductMapper.updateBizProduct(bizProduct1);

            });
        }

        return bizReceiptMapper.updateBizReceipt(bizReceipt);
    }
}
